package com.ysh.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysh.ssm.constans.ResultCode;
import com.ysh.ssm.exceptions.AddDataException;
import com.ysh.ssm.exceptions.FindDataException;
import com.ysh.ssm.mapper.UserMapper;
import com.ysh.ssm.pojo.TbUser;
import com.ysh.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 袁胜虎 on 2022/8/31 21:46
 */
@Service
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public TbUser findById(Long id) {
        //断言,当前的判断条件一定符号内部的表达式的要求,否则就会执行后面的操作
        Assert.isTrue(id>0,()->{
            throw new FindDataException("id不符合要求", ResultCode.ID_NOT_ALLOWED);
        });
        TbUser user = userMapper.findById(id);

        Assert.notNull(user,()->{
            throw new FindDataException("数据不存在", ResultCode.DATA_NOT_EXIST);
        });
        return user;
    }

    @Override
    public void addUser(TbUser tbUser) {
        Assert.isTrue(!tbUser.isEmpty(),()->{
             throw new AddDataException("数据不完整",ResultCode.DATA_NOT_ALLOWED);
        });
        userMapper.addUser(tbUser);
    }

    @Override
    public PageInfo<TbUser> findByPage(int pageSize, int pageNum) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查询数据
        List<TbUser> userList = userMapper.findUsers();
        //封装分页对象
        PageInfo<TbUser> pageInfo = new PageInfo<>(userList);

        return pageInfo;
    }

    @Override
    public TbUser findUsernameAndPassword(String username, String password) {
        Assert.isTrue(username != null && password != null,()->{
            throw new FindDataException("参数为空",ResultCode.CAN_SHU_WEI_KONG);
        });
        TbUser usernameAndPassword = userMapper.findUsernameAndPassword(username, password);
        System.err.println(usernameAndPassword);
        Assert.notNull(usernameAndPassword,()->{
            throw new FindDataException("数据不存在", ResultCode.DATA_NOT_EXIST);
        });

        return usernameAndPassword;
    }

    @Override
    public void deleteById(Long id) {
        Assert.isTrue(id>0,()->{
            throw new FindDataException("id不符合要求", ResultCode.ID_NOT_ALLOWED);
        });
        userMapper.deleteById(id);
    }

    @Override
    public void updateUser(String username, String password, String email, String gender, Long id) {
        Assert.isTrue(id>0,()->{
            throw new FindDataException("id不符合要求", ResultCode.ID_NOT_ALLOWED);
        });
        Assert.isTrue(username != null && password != null && email != null && gender != null,()->{
            throw new FindDataException("参数为空",ResultCode.CAN_SHU_WEI_KONG);
        });
        userMapper.updateUser(username,password,email,gender,id);
    }
}
